package com.wu.solon.database.lazy.starter.simple.controller;

//import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.wu.solon.database.lazy.starter.simple.domain.SysUser;
import com.wu.solon.database.lazy.starter.simple.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.data.annotation.Tran;
import org.wu.framework.core.exception.RuntimeExceptionFactory;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.web.solon.EasyController;

import javax.sql.DataSource;
import java.time.LocalDateTime;

/**
 * describe : 事物使用
 *
 * @author : Jia wei Wu
 * @version 1.0
 * @date : 2022/3/6 12:46
 */
@Api(tags = "事物使用")
@EasyController
@Controller
public class TransactionalController {
    @Inject
    private  SysUserService sysUserService;
    @Inject
    private LazyLambdaStream lazyLambdaStream;
    @Inject
    private  DataSource dataSource;



//    @DSTransactional
    @Tran()
    @ApiOperation("用户信息 事物使用")
    @Mapping("/transactional")
    public String upsertTransactional() {
        final SysUser sysUser = new SysUser();
        sysUser.setId(1L);
        sysUser.setCreateTime(LocalDateTime.now());
        sysUser.setUsername(LocalDateTime.now()+"hahah");
        sysUserService.updateById(sysUser);
        sysUser.setCreateTime(LocalDateTime.now());
        sysUser.setUsername("hahahwww");
        lazyLambdaStream.upsert(sysUser);
        return "Sucess";
    }

    @Tran()
    @ApiOperation("用户信息 事物回滚")
    @Mapping("/rollback")
    public String rollback() {
        final SysUser sysUser = new SysUser();
        sysUser.setId(1L);
        sysUser.setCreateTime(LocalDateTime.now());
        sysUser.setUsername(LocalDateTime.now()+"hahah");
        sysUserService.updateById(sysUser);
        sysUser.setCreateTime(LocalDateTime.now());
        sysUser.setUsername("hahahwww");
        lazyLambdaStream.upsert(sysUser);
        RuntimeExceptionFactory.of("rollback");
        return "Sucess";
    }

}
